﻿using Dapper;
using Estsh.Core.Base;
using Estsh.Core.Dapper;
using Estsh.Core.Model.EnumUtil;
using Estsh.Core.Model.Extend.Models;
using Estsh.Core.Models;
using Estsh.Core.Repositories;
using Estsh.Core.Wms.IRepositories;
using System.Data;
using System.Text;

namespace Estsh.Core.Wms.Repositories
{
    /// <summary>
    /// 零星出库
    /// </summary>
    public class ScatteredOutStockRepository : BaseRepository<BaseEntity>, IScatteredOutStockRepository
    {
        public ScatteredOutStockRepository(DapperDbContext _dapperDbContext) : base(_dapperDbContext)
        {

        }
        /// <summary>
        /// 获取零星出库单据信息
        /// </summary>
        /// <param name="orderNo"></param>
        /// <returns></returns>
        public List<WmsOutstock> GetScatteredOutStockOrderList(string orderNo)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                dbConn.Open();
                StringBuilder SqlStringBuilder = new StringBuilder(1024);
                SqlStringBuilder.Append("SELECT DISTINCT a.order_no,b.enum_desc AS order_type_desc,a.create_time,d.emp_no,a.factory_code,a.enabled FROM dbo.wms_outstock a (nolock) ");
                SqlStringBuilder.Append("LEFT JOIN dbo.sys_enum b (nolock) ON a.order_type = b.enum_value AND b.enum_type = 'wms_outstock_order_type' ");
                SqlStringBuilder.Append("LEFT JOIN dbo.sys_emp d (nolock) ON a.create_userid = d.emp_id ");
                SqlStringBuilder.Append("WHERE a.order_no like '%" + orderNo + "%' AND order_status in ('10','30') AND order_type = " + (int)WmsEnumUtil.MoveOrderType.EXCEPTION_OUT_STOCK + " and a.enabled = 'Y' ");

                List<WmsOutstock> wmsMoves = dbConn.Query<WmsOutstock>(SqlStringBuilder.ToString()).ToList();
                return wmsMoves;
            }
        }

        /// <summary>
        /// 获取零星出库单据信息通过订单号
        /// </summary>
        /// <param name="orderNo"></param>
        /// <returns></returns>
        public List<WmsOutstock_Extend> GetScatteredOutStockOrderListByOrderNo(string orderNo)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                dbConn.Open();
                SetObjectDetail rfInfo = new SetObjectDetail();
                StringBuilder SqlStringBuilder = new StringBuilder(1024);
                SqlStringBuilder.Append("SELECT c.item_status,a.order_no,b.enum_desc AS order_type_desc,CONVERT(INT, c.qty) qty,CONVERT(INT, c.out_qty) out_qty,c.part_no ,a.create_userid,a.create_time,c.src_zone_id,c.src_zone_name,d.emp_no,a.factory_code,p.part_spec FROM dbo.wms_outstock a (nolock) ");
                SqlStringBuilder.Append("LEFT JOIN dbo.sys_enum b (nolock) ON a.order_type=b.enum_value AND	b.enum_type='wms_outstock_order_type'  ");
                SqlStringBuilder.Append("LEFT JOIN dbo.wms_outstock_detail c (nolock) ON a.order_no=c.order_no ");
                SqlStringBuilder.Append("LEFT JOIN dbo.sys_emp d (nolock) ON a.create_userid=d.emp_id ");
                SqlStringBuilder.Append("LEFT JOIN dbo.sys_part p (nolock) ON c.part_no=p.part_no ");
                SqlStringBuilder.Append("WHERE a.order_no = '" + orderNo + "' AND order_type=" + (int)WmsEnumUtil.MoveOrderType.EXCEPTION_OUT_STOCK + " and a.enabled='Y' ");
                List<WmsOutstock_Extend> wmsMoves = dbConn.Query<WmsOutstock_Extend>(SqlStringBuilder.ToString()).ToList();
                return wmsMoves;
            }
        }
        //根据条码号获取单据信息
        public SysStock GetStockByCartonNo(string cartonNo)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                dbConn.Open();
                StringBuilder stringBuilder = new StringBuilder();
                stringBuilder.Append("SELECT a.carton_type,a.vendor_id, a.vendor_code, a.carton_no, a.locate_id, a.locate_name,a.qty, a.status, a.enabled,b.part_id,b.part_no,b.part_spec, b.part_spec, a.warehouse_id,a.warehouse_name,a.zone_id,a.zone_name,c.enum_desc AS stock_status " +
                                          ",a.unit,a.factory_id,a.factory_code,a.erp_warehouse FROM sys_stock a (nolock) LEFT JOIN dbo.sys_part b (nolock) ON a.part_no = b.part_no LEFT JOIN dbo.sys_enum c (nolock) ON a.status=c.enum_value AND enum_type ='sys_stock_status' WHERE a.carton_no='" + cartonNo + "' ");
                SysStock sysStocks = dbConn.QueryFirstOrDefault<SysStock>(stringBuilder.ToString());
                return sysStocks;
            }
        }

        public List<SysStock> GetStockByCartonNoList(string cartonNo)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                dbConn.Open();
                StringBuilder stringBuilder = new StringBuilder();
                stringBuilder.Append("SELECT a.carton_type,a.vendor_id, a.vendor_code, a.carton_no, a.locate_id, a.locate_name,a.qty, a.status, a.enabled,b.part_id,b.part_no,b.part_spec, b.part_spec, a.warehouse_id,a.warehouse_name,a.zone_id,a.zone_name,c.enum_desc AS stock_status " +
                                          ",a.unit,a.factory_id,a.factory_code,a.erp_warehouse FROM sys_stock a (nolock) LEFT JOIN dbo.sys_part b (nolock) ON a.part_no = b.part_no LEFT JOIN dbo.sys_enum c (nolock) ON a.status=c.enum_value AND enum_type ='sys_stock_status' WHERE a.carton_no in ('" + cartonNo + "') ");
                List<SysStock> sysStocks = dbConn.Query<SysStock>(stringBuilder.ToString()).ToList();
                return sysStocks;
            }
        }



        //根据单据号获取零星出库单信息
        public List<WmsOutstock> GetOutStockByOrderNo(string orderNo)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                dbConn.Open();
                StringBuilder stringBuilder = new StringBuilder(1024);
                stringBuilder.Append("SELECT * FROM dbo.wms_outstock (nolock) WHERE order_no = '" + orderNo + "' AND order_status in ('10','30') AND order_type=" + (int)WmsEnumUtil.MoveOrderType.EXCEPTION_OUT_STOCK + " and enabled='Y'");
                List<WmsOutstock> WmsOutstocks = dbConn.Query<WmsOutstock>(stringBuilder.ToString()).ToList();
                return WmsOutstocks;
            }
        }

        /// <summary>
        /// 根据单据号、erp库存地验证是否在单据内
        /// </summary>
        /// <param name="locateName"></param>
        /// <returns></returns>
        public List<WmsOutstockDetail> GetOutstockByOrderNoSrcErp(string orderNo, string srcErpWare)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                dbConn.Open();
                StringBuilder stringBuilder = new StringBuilder();
                stringBuilder.Append("SELECT * FROM dbo.wms_outstock_detail (nolock) WHERE order_no='" + orderNo + "' and src_erp_warehouse='" + srcErpWare + "' and enabled='Y'");
                List<WmsOutstockDetail> instockDetailsErp = dbConn.Query<WmsOutstockDetail>(stringBuilder.ToString()).ToList();
                return instockDetailsErp;
            }
        }
        //根据单据号、零件号验证
        public List<WmsOutstockDetail> GetOutstockByOrderNoPartNo(string orderNo, string partNo)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                dbConn.Open();
                StringBuilder stringBuilder = new StringBuilder();
                stringBuilder.Append("SELECT * FROM dbo.wms_outstock_detail (nolock) WHERE order_no='" + orderNo + "' and part_no='" + partNo + "' and enabled='Y'");
                List<WmsOutstockDetail> WmsOutstockDetails = dbConn.Query<WmsOutstockDetail>(stringBuilder.ToString()).ToList();
                return WmsOutstockDetails;
            }
        }


        public List<WmsOutstock> GetOutstockByOrderNo(string orderNo)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                dbConn.Open();
                StringBuilder SqlStringBuilder = new StringBuilder(1024);
                SqlStringBuilder.Append("SELECT a.order_no,b.enum_desc AS order_type_desc,CONVERT(INT, c.qty) qty,CONVERT(INT, c.out_qty) out_qty,c.part_no ,a.create_userid,a.create_time,c.src_zone_id,c.src_zone_name,d.emp_no,a.factory_code,p.part_spec FROM dbo.wms_outstock a (nolock) ");
                SqlStringBuilder.Append("LEFT JOIN dbo.sys_enum b (nolock) ON a.order_type=b.enum_value AND	b.enum_type='wms_outstock_order_type'  ");
                SqlStringBuilder.Append("LEFT JOIN dbo.wms_outstock_detail c (nolock) ON a.order_no=c.order_no ");
                SqlStringBuilder.Append("LEFT JOIN dbo.sys_emp d (nolock) ON a.create_userid=d.emp_id ");
                SqlStringBuilder.Append("LEFT JOIN dbo.sys_part p (nolock) ON c.part_no=p.part_no ");
                SqlStringBuilder.Append("WHERE a.order_no = '" + orderNo + "' AND order_status in ('10','30') AND order_type=" + (int)WmsEnumUtil.MoveOrderType.EXCEPTION_OUT_STOCK + " and a.enabled='Y'");
                List<WmsOutstock> wmsMoves = dbConn.Query<WmsOutstock>(SqlStringBuilder.ToString()).ToList();
                return wmsMoves;
            }
        }
        public List<WmsOutstockDetail> GetOutstockDetailByOrderNo(string orderNo)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                dbConn.Open();
                StringBuilder SqlStringBuilder = new StringBuilder(1024);
                SqlStringBuilder.Append("SELECT SUM( CONVERT(INT, c.qty)) qty,SUM(CONVERT(INT, c.out_qty)) out_qty,c.item_status,c.part_no,c.part_spec FROM dbo.wms_outstock a (nolock) ");
                SqlStringBuilder.Append("LEFT JOIN dbo.wms_outstock_detail c (nolock) ON a.order_no=c.order_no ");
                SqlStringBuilder.Append(" WHERE a.order_no = '" + orderNo + "' AND order_status in ('10','30') AND order_type=" + (int)WmsEnumUtil.MoveOrderType.EXCEPTION_OUT_STOCK + " and a.enabled='Y' group by c.part_no,c.part_spec");

                List<WmsOutstockDetail> wmsMoves1 = dbConn.Query<WmsOutstockDetail>(SqlStringBuilder.ToString()).ToList();
                return wmsMoves1;
            }
        }

        //获取完成信息提交数据
        public List<WmsOutstockDetail> GetFinishOutStockDetailByOrderNo(string orderNo)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                dbConn.Open();
                StringBuilder SqlStringBuilder = new StringBuilder(1024);
                SqlStringBuilder.Append("SELECT SUM(CONVERT(INT, c.out_qty)) out_qty,c.part_no,src_erp_warehouse,plan_date,plan_time,a.factory_code,a.order_no FROM dbo.wms_outstock a (nolock) ");
                SqlStringBuilder.Append("LEFT JOIN dbo.wms_outstock_detail c (nolock) ON a.order_no=c.order_no ");
                SqlStringBuilder.Append(" WHERE a.order_no = '" + orderNo + "' AND order_status <> '40' and a.enabled='Y' group by c.part_no,src_erp_warehouse,plan_date,plan_time,a.factory_code,a.order_no");

                List<WmsOutstockDetail> wmsMoves = dbConn.Query<WmsOutstockDetail>(SqlStringBuilder.ToString()).ToList();
                return wmsMoves;
            }
        }

        //获取零件号信息
        public SysPart GetPartNOInfoByPartNo(string partNo)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                dbConn.Open();
                StringBuilder SqlStringBuilder = new StringBuilder(1024);
                SqlStringBuilder.Append("    SELECT * from sys_part (nolock) where part_no='" + partNo + "' ");
                SysPart sysParts = dbConn.QueryFirstOrDefault<SysPart>(SqlStringBuilder.ToString());
                return sysParts;
            }
        }

        //事务批量执行添加、修改
        public int SQLTransaction(List<string> sqlStrings, List<DynamicParameters> parameterList)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                if (dbConn.State == ConnectionState.Closed)
                {
                    dbConn.Open();
                }
                //执行事务
                IDbTransaction transaction = dbConn.BeginTransaction();
                if (parameterList == null || parameterList.Count == 0)
                {
                    try
                    {
                        for (int i = 0; i < sqlStrings.Count; i++)
                        {
                            dbConn.Execute(sqlStrings[i], null, transaction);
                        }
                        transaction.Commit();
                        return 1;
                    }
                    catch (Exception exception)
                    {
                        transaction.Rollback();
                        return 0;
                    }
                }
                else
                {
                    try
                    {
                        for (int i = 0; i < sqlStrings.Count; i++)
                        {
                            dbConn.Execute(sqlStrings[i], parameterList[i], transaction);
                        }
                        transaction.Commit();
                        return 1;
                    }
                    catch (Exception exception)
                    {
                        transaction.Rollback();
                        return 0;
                    }
                }
            }
        }


        public List<WmsRackPackage_Extend> GetCartonInfo(string rackNo)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                dbConn.Open();
                string result = string.Empty;

                StringBuilder sql = new StringBuilder(1024);
                sql.Append("select * from wms_rack_package (nolock) where rack_no= @rack_no and enabled='Y'");
                DynamicParameters parameters = new DynamicParameters();
                parameters.Add("@rack_no", rackNo);

                return dbConn.Query<WmsRackPackage_Extend>(sql.ToString(), parameters).ToList();
            }
        }

        public List<WmsOutstockDetail> GetOutstockOrderNo(string orderNo)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                dbConn.Open();
                StringBuilder stringBuilder = new StringBuilder();
                stringBuilder.Append("SELECT * FROM dbo.wms_outstock_detail (nolock) WHERE order_no='" + orderNo + "'  and enabled='Y'");
                List<WmsOutstockDetail> instockDetailsErp = dbConn.Query<WmsOutstockDetail>(stringBuilder.ToString()).ToList();
                return instockDetailsErp;
            }
        }

    }
}
